import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class ScrollPage extends StatefulWidget {
  ScrollPage({Key key}) : super(key: key);

  _ScrollPageState createState() => _ScrollPageState();
}

class _ScrollPageState extends State<ScrollPage> {
  List list =[];
  int _page = 1;
  bool hasMore = true;
  bool onload  = false;
  ScrollController _scrollController = new ScrollController();
  @override
  void initState() {
    super.initState();
    this._getData();
    _scrollController.addListener((){
      //  _scrollController.position.pixels //滚动条下拉距离
      //  _scrollController.position.maxScrollExtent  // 整个页面的高度
       if(_scrollController.position.pixels > _scrollController.position.maxScrollExtent - 40){
          this._getData();
       }
    });
  }
  _getData() async{
    if(this.hasMore && !this.onload){
      setState(() {
        // 防止数据多次请求
        this.onload = true;
      });
      var apiUrl = 'https://xcx.qzriji.com/server/public/index/index/bookList?uid=6FvyRLTf2I&typeID=4&size=15&p=${_page}';
      Response result = await Dio().get(apiUrl);
      if(result.statusCode == 200){
        print(apiUrl);
        var res = result.data['data'];
        setState(() {
          this.list.addAll(res);
          this._page++;
          this.onload = false;
          if(res.length < 15){
            this.hasMore = false;
          }
        }); 
      }else{
        print(result.statusCode);
      }
    }
    
  }
  //下拉刷新回调   //Future<void> 必加
  Future<void> _onRefresh() async{
    await Future.delayed((Duration(milliseconds: 100)),(){
      setState(() {
        this.list = [];
        this._page = 1;
        this.onload = false;
        this.hasMore = true;
        _getData();
      }); 
      
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ScrollPage"),
      ),
      body: this.list.length > 0 ? 
        RefreshIndicator(
          onRefresh: _onRefresh,
          child: ListView.builder(
            controller: _scrollController,
              itemCount: this.list.length,
              itemBuilder: (context,index){
                // 判断是否拉到底了 如果
                if(index == this.list.length - 1){
                  return Column(
                      children: <Widget>[
                        ListTile(
                          title: Text(this.list[index]['name']),
                        ),
                        Divider(),
                        _getMoreWidget()
                    ],
                  );
                }else{
                  return Column(
                      children: <Widget>[
                        ListTile(
                          title: Text(this.list[index]['name']),
                        )
                    ],
                  );
                }
                
              },
            ),
        ) : _getMoreWidget()
      ,
    );
  }

  
  //加载中的圈圈
  Widget _getMoreWidget() {
    if(hasMore){
      return Center(
        child: Padding(
          padding: EdgeInsets.all(10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(
                '加载中...',
                style: TextStyle(fontSize: 16.0),
              ),
              Container(
                margin: EdgeInsets.only(left:20),
                width: 18,
                height: 18,
                child: CircularProgressIndicator(
                  strokeWidth: 2.0,
                ),
              )
            ],
          ),
        ),
      );
    }else if(!hasMore){
       return Center(
         child: Padding(
           padding: EdgeInsets.all(10.0),
           child: Text("--我是有底线的--"),
         ),
       );
    }
  }
}
 

 